﻿using Yxinmt.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows;

namespace Yxinmt.Controls
{
    public class ScrollViewer : BaseControl, IControl
    {
        private System.Windows.Controls.ScrollViewer _ScrollViewer { get; set; }

        public ScrollViewer(Dictionary<string, object> property, IComponent component)
        {
            Property = property;
            _Component = component;

            _ScrollViewer = new System.Windows.Controls.ScrollViewer();
            this.Element = _ScrollViewer;

            InitSet();
            this.SetProperty();
            this.SetControlProperty(false, false);

            _Window.ScrollViewerList.Add(this);

            ApplyCustomScrollBarWidth();
        }

        public override void Add(System.Windows.FrameworkElement control)
        {
            _ScrollViewer.Content = control;
        }

        private void SetProperty()
        {
            this.SetDefaultValue("ScrollX", "Auto");
            this.SetDefaultValue("ScrollY", "Auto");
            
        }

        public void SetIsScroll(bool blScroll)
        {
            if (blScroll)
            {
                _ScrollViewer.HorizontalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto;
                _ScrollViewer.VerticalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto;
            }
            else
            {
                _ScrollViewer.HorizontalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Disabled;
                _ScrollViewer.VerticalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Disabled;
            }
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.ScrollViewer c = _ScrollViewer;
            switch (name)
            {
                case "name": c.Name = string.Concat("scroll_", (string)value); break;
                case "Height": c.Height = Convert.ToDouble(value); break;
                case "Width": c.Width = Convert.ToDouble(value); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "BackColor": c.Background = GetColor((string)value); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Visible": SetVisible((bool)value); break;
                case "GridRowColumn": SetGridRowColumn((string)value); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "ScrollX": c.HorizontalScrollBarVisibility = GetScrollBarVisibility((string)value); break;
                case "ScrollY": c.VerticalScrollBarVisibility = GetScrollBarVisibility((string)value); break;
                case "ZIndex": SetZIndex((int)value); break;
            }
        }

        public void ScrollToBottom()
        {
            _ScrollViewer.ScrollToBottom();
        }

        public static void ApplyCustomScrollBarWidth()
        {
            // 创建样式
            Style scrollViewerStyle = new Style(typeof(System.Windows.Controls.ScrollViewer));

            // 设置垂直滚动条宽度为8
            scrollViewerStyle.Resources.Add(
                SystemParameters.VerticalScrollBarWidthKey,
                40.0);

            // 设置水平滚动条高度为8
            scrollViewerStyle.Resources.Add(
                SystemParameters.HorizontalScrollBarHeightKey,
                40.0);

            // 将样式应用到应用程序资源
            Application.Current.Resources[typeof(System.Windows.Controls.ScrollViewer)] = scrollViewerStyle;
        }

    }
}
